package editor.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;


import map.MapObject;
import map.RasterCell;

public class SidePanel extends JPanel implements InfoContainer{
	
	JLabel infocontainer;
	
	ArrayList<Component> propertieComponents;
	ArrayList<MapObject> activeCells;
	
	HashMap<JTextField, String> currentProperties;
	
	public SidePanel(){
		super();
		//setBackground(Color.LIGHT_GRAY);
		setPreferredSize(new Dimension(175,100));
		//setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
		setLayout(new FlowLayout());
		infocontainer = new JLabel("", SwingConstants.LEFT);
		infocontainer.setSize(0, 50);
		add(infocontainer);
		
		propertieComponents = new ArrayList<Component>();
		activeCells = new ArrayList<MapObject>();
		currentProperties = new HashMap<JTextField, String>();
	}

	@Override
	public void print(MapObject arg) {
		if(arg != null){			
			String titlte = arg.getClass().getSimpleName() + "[" +arg.getCurrentX() + "][" +arg.getCurrentY() +" ]";
			
			infocontainer.setText(wrapHTML(titlte));
		}

	}
	
	public void printProperties(){
		
	}

	private String wrapHTML(String text){
		return "<html><head></head><body>"+ text +"</body></html>";
	}

	public void printDetails(final MapObject arg){
		printDetails(arg, arg.toString(), new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				HashMap<String, String> newProperties = new HashMap<String, String>();
				for(Entry entry : currentProperties.entrySet()){
					JTextField source = (JTextField)entry.getKey();
					newProperties.put((String)entry.getValue(), source.getText());
					
				}
				arg.setProperties(newProperties);
			}
		});
	}
	
	public void printDetails(MapObject arg, String title, ActionListener savelistener) {
		clearDetails();
		
		
		JLabel className = new JLabel(title);
		propertieComponents.add(className);
		add(className);

		
		for(Entry entry : arg.getProperties().entrySet()){
			JPanel row = new JPanel();
			//row.setBorder(BorderFactory.createLoweredBevelBorder());
			row.setLayout(new FlowLayout(FlowLayout.LEFT));
			
			JLabel propertyName = new JLabel();
			propertyName.setText(entry.getKey().toString());
			row.add(propertyName);
			
			
			JTextField propertyValue = new JTextField();
			propertyValue.setColumns(10);
			propertyValue.setText(entry.getValue().toString());
		
			row.add(propertyValue);
			
			currentProperties.put(propertyValue, (String)entry.getKey());
			
			propertieComponents.add(row);
			add(row);
		}

		if(propertieComponents.size() > 1){
			JButton savePropertieChange = new JButton("save");
			savePropertieChange.addActionListener(savelistener);
			
			propertieComponents.add(savePropertieChange);
			add(savePropertieChange);
		}
		
	}


	public void printDetails(final ArrayList<RasterCell> workList) {			
			ActionListener saveHandler = new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					for(RasterCell cell : workList){
						
						
						HashMap<String, String> newProperties = new HashMap<String, String>();
						for(Entry entry : currentProperties.entrySet()){
							JTextField source = (JTextField)entry.getKey();
							newProperties.put((String)entry.getValue(), source.getText());
							
						}
						cell.getAttachedObject().setProperties(newProperties);
						
						
					}
				}
			};
			MapObject leftCorner = workList.get(0).getAttachedObject();
			MapObject rightCorner = workList.get(workList.size()-1).getAttachedObject();
			
			String title = leftCorner.getClass().getSimpleName() +" ["+leftCorner.getCurrentX()+"]["+leftCorner.getCurrentY()+"] - ["+rightCorner.getCurrentX()+"]["+rightCorner.getCurrentY()+"]";
			printDetails(leftCorner, title, saveHandler);
	}

	public void clearDetails() {
		for(Component comp : propertieComponents){
			remove(comp);
		}
		propertieComponents.clear();
		currentProperties.clear();
	}
}
